package cease.update;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import cease.butter.Constants;
import cease.http.HttpRequestExecutor;
import cease.http.HttpRequestResult;

/**
 * @author dhf
 */
public class Version {
    private Version() {}

    private static HttpRequestExecutor executor = new HttpRequestExecutor();

    private static Pattern versionPattern = Pattern
            .compile("(\\d)\\.(\\d)\\.(\\d)");

    private int majorVersion;

    private int subVersion;

    private int revision;

    public static Version parse(String version) throws UpdateException {
        version = StringUtils.trimToEmpty(version);
        Matcher m = versionPattern.matcher(version);
        if (m.matches()) {
            int i = 1;
            Version v = new Version();
            v.majorVersion = Integer.parseInt(m.group(i++));
            v.subVersion = Integer.parseInt(m.group(i++));
            v.revision = Integer.parseInt(m.group(i++));
            return v;
        } else {
            throw new UpdateException("Illegal version format: " + version);
        }
    }

    public static Version getLastestVersion() throws UpdateException {
        try {
            HttpRequestResult result = executor.get(Constants.APP_VERSION_URL,
                    null, null, null);
            if (result.getStatus() == 200) {
                String version = StringUtils
                        .trimToEmpty(result.getContentStr());
                Matcher m = versionPattern.matcher(version);
                if (m.matches()) {
                    int i = 1;
                    Version v = new Version();
                    v.majorVersion = Integer.parseInt(m.group(i++));
                    v.subVersion = Integer.parseInt(m.group(i++));
                    v.revision = Integer.parseInt(m.group(i++));
                    return v;
                } else {
                    throw new UpdateException("Illegal version file: "
                            + Constants.APP_VERSION_URL);
                }
            } else {
                throw new UpdateException("Http Status Code : "
                        + result.getStatus());
            }
        } catch (IOException e) {
            throw new UpdateException(e);
        }
    }

    public static Version getCurrentVersion() throws UpdateException {
        String version = StringUtils.trimToEmpty(Constants.APP_VERSION);
        Matcher m = versionPattern.matcher(version);
        if (m.matches()) {
            int i = 1;
            Version v = new Version();
            v.majorVersion = Integer.parseInt(m.group(i++));
            v.subVersion = Integer.parseInt(m.group(i++));
            v.revision = Integer.parseInt(m.group(i++));
            return v;
        } else {
            throw new UpdateException("Illegal version format: "
                    + Constants.APP_VERSION);
        }

    }

    @Override
    public String toString() {
        return majorVersion + "." + subVersion + "." + revision;
    }

    public int getMajorVersion() {
        return majorVersion;
    }

    public int getSubVersion() {
        return subVersion;
    }

    public int getRevision() {
        return revision;
    }
}
